import 'package:catguard/global/global_data.dart';
import 'package:catguard/pages/home/home_logic.dart';
import 'package:catguard/views/common_widget.dart';
import 'package:catguard/views/dialog_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:line_awesome_flutter/line_awesome_flutter.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../config/config.dart';
import '../../../generated/assets.dart';
import '../../../routers/routers.dart';

class FragmentMine extends StatelessWidget {
  FragmentMine({super.key});

  final logic = Get.find<HomeLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg,
      resizeToAvoidBottomInset: false,
      body: SafeArea(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 40.w),
          child: Column(
            children: [
              SizedBox(height: 60.w),
              //头像区
              GestureDetector(
                onTap: () {
                  Get.toNamed(Routes.myInfo);
                },
                child: Row(
                  children: [
                    Container(
                      width: 110.w,
                      height: 110.w,
                      decoration: BoxDecoration(
                          color: Config.colorText3.withOpacity(0.2),
                          borderRadius: BorderRadius.circular(60.w)),
                      child: Icon(
                        LineAwesomeIcons.user,
                        size: 60.w,
                        color: Config.colorText3.withOpacity(0.6),
                      ),
                    ),
                    SizedBox(width: 20.w),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '用户8061',
                          style: TextStyle(
                              color: Config.colorText1, fontSize: Config.textSizeTitle.sp),
                        ),
                        SizedBox(height: 10.w),
                        Text(
                          '1台设备    1只猫咪',
                          style:
                              TextStyle(color: Config.colorText2, fontSize: Config.textSizeInfo.sp),
                        ),
                      ],
                    ),
                    Expanded(
                      child: Container(
                        alignment: Alignment.centerRight,
                        child: Icon(
                          LineAwesomeIcons.pen_fancy_solid,
                          size: 66.w,
                          color: Config.colorText2,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              //我的宠物
              Container(
                width: double.infinity,
                margin: EdgeInsets.only(top: 38.w),
                height: 288.w,
                padding: EdgeInsets.only(top: 30.w, bottom: 30.w,left: 33.w,right: 0.w),
                decoration: BoxDecoration(
                  color: Config.colorBg2,
                  borderRadius: BorderRadius.circular(Config.roundBig.w),
                ),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '我的宠物',
                          style: TextStyle(
                              color: Config.colorText1, fontSize: Config.textSizeTitle.sp),
                        ),
                        // Row(
                        //   children: [
                        //     Text(
                        //       '查看全部',
                        //       style: TextStyle(
                        //           color: Config.colorText3,
                        //           fontSize: Config.textSizeContent.sp,
                        //           height: 1),
                        //     ),
                        //     Icon(
                        //       Icons.arrow_forward_ios_rounded,
                        //       size: 24.w,
                        //       color: Config.colorText3,
                        //     ),
                        //   ],
                        // ),
                      ],
                    ),
                    SizedBox(height: 35.w),
                    SizedBox(
                      height: 140.w,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Obx(
                            () => ListView.builder(
                                itemCount: global.pets.length,
                                physics: const NeverScrollableScrollPhysics(),
                                scrollDirection: Axis.horizontal,
                                shrinkWrap: true,
                                itemBuilder: (context, index) {
                                  return Container(
                                    padding: EdgeInsets.only(right: 30.w),
                                    child: Column(
                                      children: [
                                        Container(
                                          width: 94.w,
                                          height: 94.w,
                                          padding: EdgeInsets.all(8.w),
                                          decoration: BoxDecoration(
                                              color: Config.colorMain.withOpacity(0.2),
                                              borderRadius: BorderRadius.circular(50.w)),
                                          child: Image.asset(Assets.imagesPetNormal),
                                        ),
                                        SizedBox(height: 12.w),
                                        Text(
                                          global.pets[index]['name'],
                                          style: TextStyle(
                                              color: Config.colorText2,
                                              fontSize: Config.textSizeMinor.sp),
                                        ),
                                      ],
                                    ),
                                  );
                                }),
                          ),
                          Obx(
                            () => Visibility(
                              visible: global.pets.length < 5,
                              child: GestureDetector(
                                onTap: () {
                                  Get.toNamed(Routes.addPet);
                                },
                                child: Container(
                                  width: 90.w,
                                  height: 90.w,
                                  padding: EdgeInsets.all(5.w),
                                  decoration: BoxDecoration(
                                      color: Config.colorMain.withOpacity(0.2),
                                      borderRadius: BorderRadius.circular(45.w)),
                                  child: Icon(
                                    Icons.add,
                                    color: Config.colorMain,
                                    size: 55.w,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
              //菜单
              Container(
                width: double.infinity,
                margin: EdgeInsets.only(top: 30.w),
                padding: EdgeInsets.symmetric(horizontal: 35.w, vertical: 4.w),
                decoration: BoxDecoration(
                  color: Config.colorBg2,
                  borderRadius: BorderRadius.circular(Config.roundBig.w),
                ),
                child: ListView(
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  children: [
                    menuItem("消息中心", Icons.notifications_active_outlined, () {
                      Get.toNamed(Routes.msgCenter);
                    }),
                    CommonWidget.commonDivider(),
                    menuItem("家庭共享", Icons.home_mini_rounded, () {
                      Get.toNamed(Routes.familyShare);
                    }),
                    CommonWidget.commonDivider(),
                    menuItem("设置", Icons.settings_outlined, () {
                      Get.toNamed(Routes.setting);
                    }),
                    CommonWidget.commonDivider(),
                    menuItem("联系客服", Icons.phone_in_talk_rounded, () {
                      DialogManager.showDialog("确认打开客服链接\nhttps://t.me/SamWarrior", (){
                        launchUrl(Uri.parse('https://t.me/SamWarrior'));
                      });
                    }),
                    CommonWidget.commonDivider(),
                    menuItem("关于", Icons.info_outline, () {
                      Get.toNamed(Routes.aboutUs);
                    }),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  menuItem(String title, IconData icon, Function() onTap) {
    return InkWell(
      onTap: onTap,
      child: SizedBox(
        height: 114.w,
        child: Row(
          children: [
            Icon(
              icon,
              size: 50.w,
              color: Config.colorText1,
            ),
            SizedBox(width: 20.w),
            Text(
              title,
              style: TextStyle(color: Config.colorText1, fontSize: Config.textSizeSubTitle.sp),
            ),
            Expanded(
                child: Container(
              alignment: Alignment.centerRight,
              child: Icon(
                Icons.arrow_forward_ios_rounded,
                color: Config.colorText3.withOpacity(0.7),
                size: 32.w,
              ),
            ))
          ],
        ),
      ),
    );
  }
}
